package main

import (
  "net"
  "fmt"
)
type Server struct {
  Ip string
  Port int
}

//create a server interface
func NewServer(ip string, port int) *Server{
  server := &Server{
    Ip: ip,
    Port: port,
  }
  return server
}

func (this *Server) Handle(conn net.Conn){
  //... 
  fmt.Println("socket connected.")
}

//launch a server
func(this *Server) Start(){
  // socket listen
  fmt.Println("this: ",this.Ip, this.Port)
  listenner,err := net.Listen("tcp", fmt.Sprintf("%s:%d",this.Ip,this.Port))
  if err != nil {
    fmt.Println("net.Listen err: ",err)
    return
  }
  // close listen socket
  defer listenner.Close()

  for {
    // accept
    conn, err := listenner.Accept()
    if err != nil {
      fmt.Println("net.Accept err:", err)
      continue
    }
    // do handler
    go this.Handle(conn)
  }
}
